<?php
namespace app\common\business;

use app\common\model\mysql\SpecsValue as specsValueModel;
class SpecsValue{
	protected $model = null;
	public function __construct(){
		$this->model = new specsValueModel();
	}
 
	// 添加规格数据
	public function add($data){
		$data['status'] = config('status.mysql.normal');
		//首先根据name判断是否该规格名存在 todo
		try{
			$this->model->save($data);
		}catch(\Exception $e){
			// 记录日志
			return false;
		}
		
		//返回主键ID
		return $this->model->id;
	}

	public function getBySpecsId($specsId){
		try{
			$res = $this->model->getNormalBySpecsId($specsId,'id,name');
		}catch(\Exception $e){
			//记录日志
			return [];
		}

		return $res->toArray();
	}
	
	//状态更新
	public function status($id,$status){
		//查询id对应的记录是否存在
		$res = $this->getById($id);
		if(!$res){
			throw new \think\Exception("不存在该记录");
		}
		if($res['status'] == $status){
			throw new \think\Exception("状态没变");
		}
		$data = [
			'status' => $status,
		];

		try{
			$result = $this->model->updateById($id,$data);
		}catch(\Exception $e){
			//记录日志
			return false;
		}

		return $result;
	}

	//获取正常一级分类
	public function getNormalByPid($pid = 0,$field = 'id,name,pid'){
		try{
			$res = $this->model->getNormalByPid($pid,$field);
		}catch(\Exception $e){
			// 记录日志
			return [];
		}

		return $res->toArray();
	}

	
	// 根据ID获取记录
	public function getById($id){
		$result = $this->model->find($id);
		if(empty($result)){
			return [];
		}
		return $result->toArray();
	}
	
	//商品详情的sku规格获取
	public function dealGoodsSkus($gids,$flagVal){
		$specsValueKeys = array_keys($gids);
		//dump($specsValueKeys);exit;
		foreach ($specsValueKeys as $key => $value) {
			$specsValueKey = explode(',', $value);
			foreach ($specsValueKey as $k => $v) {
				$new[$k][] = $v;
				$specsValueIds[] = $v;
			}
		}
		$specsValueIds = array_unique($specsValueIds);
		//dump($specsValueIds);exit;
		$specsValues = $this->getNormalInIds($specsValueIds); 
		$flagVal = explode(',', $flagVal);

		$result = [];
		foreach ($new as $key => $newVal) {
			$newVal = array_unique($newVal);
			$list = [];
			foreach ($newVal as $v) {
				$list[] = [
					'id'=>$v,
					'name'=>$specsValues[$v]['name'],
					'flag'=>in_array($v, $flagVal) ? 1 : 0,
				];
			}

			$result[$key] = [
				'name' => $specsValues[$newVal[0]]['specs_name'],
				'list' => $list,
			];
		}

		return $result;
	}

	public function getNormalInIds($ids){
		if(!$ids){
			return [];
		}
		try{
			$result = $this->model->getNormalInIds($ids); 
		}catch(\Exception $e){
			return [];
		}
		$result = $result->toArray();
		if(!$result){
			return [];
		}
		//dump($result);exit;
		$specsNames = config('specs');
		//dump($specsNames);
		$specsNamesArr = array_column($specsNames, 'name','id');
		//dump($specsNamesArr);exit;
		$res = [];
		foreach ($result as $key => $value) {
			$res[$value['id']] = [
				'name' => $value['name'],
				'specs_name' => $specsNamesArr[$value['specs_id']],
			];
		}
		//dump($res);exit;
		return $res;
	}

	//
	public function dealSpecsValue($skuIdSpecsValueIds){
		//dump($skuIdSpecsValueIds);//exit;
		$ids = array_values($skuIdSpecsValueIds);
 		$ids = implode(',', $ids);
 		$ids = explode(',', $ids);
 		$res = $this->getNormalInIds($ids);
 		//dump($res);exit;
 		if(!$res){
 			return [];
 		}
 		//dump($res);//exit;
 		$result = [];
 		foreach ($skuIdSpecsValueIds as $skuId => $specs) {
 			$specs = explode(',', $specs);
 			$skuStr = [];
 			foreach ($specs as $spec) {
 				$skuStr[] = $res[$spec]['specs_name'].':'.$res[$spec]['name'];
 			}
 			$result[$skuId] = implode(' ', $skuStr);
 			//dump($skuStr);
 		}
 		//dump($result);exit;
 		return $result;
	}
}